/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.pig4cloud.pig.demo.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.demo.entity.XlmTask;
import com.pig4cloud.pig.demo.mapper.XlmTaskMapper;
import com.pig4cloud.pig.demo.service.XlmTaskService;
import com.pig4cloud.pig.demo.util.ListPageUtils;
import com.pig4cloud.pig.demo.util.MapUtils;
import org.gavaghan.geodesy.Ellipsoid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 任务
 *
 * @author xiaoxiong
 * @date 2022-04-08 18:01:37
 */
@Service
public class XlmTaskServiceImpl extends ServiceImpl<XlmTaskMapper, XlmTask> implements XlmTaskService {
    @Autowired
    XlmTaskMapper xlmTaskMapper;

    public IPage<XlmTask> getuserTaskpage(Page<XlmTask> page, String longitude, String latitude, XlmTask xlmTask) {
//        xlmTaskMapper.selectList(new QueryWrapper<XlmTask>(xlmTask));
        List<XlmTask> xlmTasks = xlmTaskMapper.getuserTasklist();

        java.text.DecimalFormat df = new java.text.DecimalFormat("#0.00");

        List<XlmTask> collect = xlmTasks.stream().sorted(Comparator.comparing((XlmTask t) ->
                {
                    double v = MapUtils.getDistanceMeter(t.getLatitude(), t.getLongitude(), latitude, longitude, Ellipsoid.Sphere);
                    if (v < 1000) {
                        t.setDistance(String.valueOf(df.format(v)) + "m");
                    } else {
                        t.setDistance(String.valueOf(df.format(v / 1000)) + "km");
                    }
                    t.setDistancem(v);
                    return v;
                }))
                .filter((XlmTask t) -> t.getDistancem() <= 50000000).collect(Collectors.toList());
        Page pages = ListPageUtils.getPages((int) page.getCurrent(), (int) page.getSize(), collect);
        return pages;
    }

    public Integer getusertoday(String userid, Integer taskid) {
        return xlmTaskMapper.getusertoday(userid,taskid);
    }

}
